public class Solution82 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode deleteDuplicates(ListNode head) {
        while (head!=null){
            if (head.next!=null && head.val==head.next.val){
                ListNode listNode=head.next;
                while (listNode.next!=null && listNode.next.val==listNode.val){
                    listNode=listNode.next;
                }
                head=listNode.next;
            }else{
                break;
            }
        }
        if (head==null){
            return null;
        }
        ListNode n=head;
        while (n!=null){
            if (n.next!=null && n.next.next!=null && n.next.val==n.next.next.val){
                ListNode m=n.next.next;
                while (m.next!=null && m.val==m.next.val){
                    m=m.next;
                }
                n.next=m.next;
            }else{
                n=n.next;
            }
        }
        return head;
    }

    public static void main(String[] args) {
        System.out.println(new Solution82().deleteDuplicates(new Solution82.ListNode(1,new Solution82.ListNode(1,new Solution82.ListNode(2)))).val);
    }
}
